"""
Case Type   : CBM
Case Name   : 设置CBM参数cbm_xlog_files_epoch非合理取值验证
Create At   : 2025/4
Owner       : songshixuan
Description :
    1、查看参数默认值
    2、修改参数值为超下边界值0
    3、修改参数值为超上边界值17
    4、修改参数值为非数值型的其他值
Expect      :
    1、参数默认值为1
    2、修改失败
    3、修改失败
    4、修改失败
History     :
    1、2025-04-07 创建
"""

import os
import unittest

from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class SharedStorage(unittest.TestCase):

    def setUp(self):
        self.log = Logger()
        self.log.info(f'----{os.path.basename(__file__)} start----')
        self.pri_sh = CommonSH('PrimaryDbUser')
        self.constant = Constant()

        self.param = 'cbm_xlog_files_epoch'

    def test_shared_storage(self):
        text = '-----step1:查看参数默认值;  expect:参数默认值为1-----'
        self.log.info(text)

        value = self.pri_sh.execut_db_sql(f'show {self.param}', dbname='postgres')
        self.param_default = value.splitlines()[-2].strip()
        self.log.info(self.param_default)
        self.assertEqual('1', self.param_default, '参数默认值校验失败')

        text = '-----step2:修改参数值为超下边界值0;  expect:修改失败-----'
        self.log.info(text)

        value = self.pri_sh.execut_db_sql(f"alter system set {self.param} to 0;", dbname='postgres')
        self.log.info(value)
        self.assertIn('outside the valid range', value, '修改成功')

        text = '-----step3:gs_guc set方式修改参数值为超上边界值17;  expect:修改失败-----'
        self.log.info(text)

        value = self.pri_sh.execute_gsguc('set', self.constant.GSGUC_SUCCESS_MSG,
                                          f"{self.param} = 17", get_detail=True)
        self.log.info(value)
        self.assertIn('outside the valid range', value, '修改成功')

        text = '-----step4:修改参数值为非边界值的正常值;  expect:修改失败-----'
        self.log.info(text)

        value = self.pri_sh.execute_gsguc('set', self.constant.GSGUC_SUCCESS_MSG,
                                          f"{self.param} = true", get_detail=True)
        self.log.info(value)
        self.assertIn('requires an integer value', value, '修改成功')

    def tearDown(self):
        self.log.info('------No Need to Recover-----')
        self.log.info(f'----{os.path.basename(__file__)} end----')
